﻿namespace NEvoWeb.Modules.NB_Store
{
    using DotNetNuke.Entities.Users;
    using DotNetNuke.Framework;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections;
    using System.Runtime.CompilerServices;
    using System.Runtime.InteropServices;
    using System.Web.UI.WebControls;
    using System.Xml;

    public class CustomForm : UserControlBase
    {
        private string _DisplayTemplateName;
        [AccessedThroughProperty("dlCustomForm")]
        private DataList _dlCustomForm;
        private bool _GenXMLrun;
        private bool _ReportForm;
        protected UserInfo _UserInfo;
        private string _XMLData;

        public CustomForm()
        {
            base.Init += new EventHandler(this.Page_Init);
            base.Load += new EventHandler(this.Page_Load);
            this._ReportForm = false;
            this._XMLData = "";
            this._GenXMLrun = false;
        }

        public string getField(string ctrlID, string CtrlType)
        {
            XmlDataDocument document = new XmlDataDocument();
            string xPath = "";
            if (!this._GenXMLrun)
            {
                if (this.dlCustomForm.Controls.Count == 0)
                {
                    this.PopualteCustomForm();
                }
                this._XMLData = SharedFunctions.getGenXML(this.dlCustomForm, 0, "en-GB");
                this._GenXMLrun = true;
            }
            if ((ctrlID == null) | (this._XMLData == null))
            {
                return "";
            }
            xPath = "genxml/" + CtrlType.ToString() + "/" + ctrlID.ToLower();
            string str2 = "";
            try
            {
                str2 = SharedFunctions.getGenXMLvalue(this._XMLData, xPath);
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                ProjectData.ClearProjectError();
            }
            return str2;
        }

        private void Page_Init(object sender, EventArgs e)
        {
            this.dlCustomForm = new DataList();
            if (this._DisplayTemplateName != null)
            {
                string s = SharedFunctions.GetStoreSettingText(this.PortalSettings.PortalId, this._DisplayTemplateName, SharedFunctions.GetCurrentCulture());
                if (s != "")
                {
                    this.dlCustomForm.ItemTemplate = new GenXMLTemplate(this.Server.HtmlDecode(s), this._UserInfo);
                    this.dlCustomForm.Visible = true;
                }
                else
                {
                    this.dlCustomForm.Visible = false;
                }
            }
            this.Controls.Add(this.dlCustomForm);
            if (this._ReportForm)
            {
                int num = -1;
                if ((this.Request.QueryString["RepID"] != null) && Versioned.IsNumeric(this.Request.QueryString["RepID"]))
                {
                    num = Conversions.ToInteger(this.Request.QueryString["RepID"]);
                }
                bool flag = false;
                if (this.Request.QueryString["frm"] != null)
                {
                    flag = true;
                }
                if (((num >= 0) & flag) & (this.dlCustomForm != null))
                {
                    string templateText = new SQLReportController().BuildReportInputForm(Conversions.ToString(num));
                    if (templateText != "")
                    {
                        this.dlCustomForm.ItemTemplate = new GenXMLTemplate(templateText);
                        this.dlCustomForm.Visible = true;
                    }
                    else
                    {
                        this.dlCustomForm.Visible = false;
                    }
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                this.PopualteCustomForm();
            }
        }

        private void PopualteCustomForm()
        {
            XMLDataInfo info = new XMLDataInfo();
            if (this._XMLData == null)
            {
                this._XMLData = "";
            }
            info.XMLData = this._XMLData;
            ArrayList list = new ArrayList();
            list.Add(info);
            this._GenXMLrun = false;
            this.dlCustomForm.DataSource = list;
            this.dlCustomForm.DataBind();
        }

        public void ReplaceField(string ctrlID, string CtrlType, string NewValue, bool cdata = true)
        {
            XmlDataDocument xmlDoc = new XmlDataDocument();
            string xPath = "";
            if ((ctrlID != null) & (this._XMLData != null))
            {
                xPath = "genxml/" + CtrlType.ToString() + "/" + ctrlID.ToLower();
                if (this._XMLData == "")
                {
                    this.PopualteCustomForm();
                    this._XMLData = SharedFunctions.getGenXML(this.dlCustomForm, 0, "en-GB");
                }
                xmlDoc.LoadXml(this._XMLData);
                SharedFunctions.ReplaceXMLNode(xmlDoc, xPath, NewValue, cdata);
                this._XMLData = xmlDoc.OuterXml;
            }
        }

        public string DisplayTemplateName
        {
            set
            {
                this._DisplayTemplateName = value;
            }
        }

        protected virtual DataList dlCustomForm
        {
            get
            {
                return this._dlCustomForm;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._dlCustomForm = value;
            }
        }

        public string ReportForm
        {
            set
            {
                try
                {
                    this._ReportForm = Conversions.ToBoolean(value);
                }
                catch (Exception exception1)
                {
                    ProjectData.SetProjectError(exception1);
                    Exception exception = exception1;
                    this._ReportForm = false;
                    ProjectData.ClearProjectError();
                }
            }
        }

        public UserInfo UsrInfo
        {
            set
            {
                this._UserInfo = value;
            }
        }

        public string XMLData
        {
            get
            {
                this._XMLData = SharedFunctions.getGenXML(this.dlCustomForm, 0, "en-GB");
                this._GenXMLrun = true;
                return this._XMLData;
            }
            set
            {
                this._XMLData = value;
            }
        }

        private class XMLDataInfo
        {
            private string _XMLData;

            public string XMLData
            {
                get
                {
                    return this._XMLData;
                }
                set
                {
                    this._XMLData = value;
                }
            }
        }
    }
}

